package fun.shuofeng.juc.callable;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @author 朔风
 * @date 2024-02-01 16:07
 */
public class Demo1 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {

        // 是用Runnable方式川创建线程
        // new Thread(new MyThread1(), "AA").start();

        // 使用Callable方式创建线程
        FutureTask<Double> future = new FutureTask<>(new MyThread2());

        new Thread(future, "Future线程").start();

        while (!future.isDone()) {
            System.out.println("waiting.....");
        }

        System.out.println(future.get());


    }
}

class MyThread1 implements Runnable {

    @Override
    public void run() {

    }
}

class MyThread2 implements Callable<Double> {

    @Override
    public Double call() throws Exception {
        System.out.println(Thread.currentThread().getName() + " come in");
        double res = 1.;
        for (int i = 1; i < 9999999; i++) {
            for (int j = 1; j < 9999999; j++) {
                res = res + j * i;
            }
        }

        return res;
    }
}
